package com.ysh.c3p0;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
    C3p0数据库连接池操作
        会自动加载配置文件信息，名称有要求不能改变
 */
public class C3p0Test1 {
    public static void main(String[] args) throws SQLException {
        findAll();
    }
    public static void findAll() throws SQLException {
        // 1.创建c3p0的数据库连接池对象
        DataSource dataSource = new ComboPooledDataSource();
        // 2.通过连接池对象获取到数据库连接
        Connection conn = dataSource.getConnection();
        // 3.执行操作
        String sql = "select * from student";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        // 4.执行sql语句，接收结果集
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            System.out.println(
                    rs.getInt("sid") + "\t"
                            + rs.getString("name") + "\t"
                            + rs.getInt("age") + "\t"
                            + rs.getDate("birthday")
            );
        }

        // 释放资源
        rs.close();
        pstmt.close();
        conn.close();    // 将连接对象归还到池中
    }
}
